package WIAB::Controller::Forum;

use strict;
use warnings;
use base 'CatalystX::Controller::reCAPTCHA';

=head1 NAME

WIAB::Controller::Forum - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index 

=cut

sub index : Private {
    my ( $self, $c ) = @_;
    $c->forward('captcha_get');
    $c->forward('get_forum_entries');
    $c->stash->{template} = 'forum.tt';
}

sub get_forum_entries : Private {
    my ($self, $c) = @_;
    my $dir = Path::Class::Dir->new($c->config->{forum});
    my $files = $c->model('Forum')->get_forum_file_list($dir);
    $c->stash->{stories} = $c->model('Forum')->get_content($files);
}


sub permalink : Local {
    my ($self, $c, $file) = @_;
    $c->stash->{permalink} = 1;
    $file = [Path::Class::Dir->new($c->config->{forum})->file($file)];
    $c->stash->{stories} = $c->model('Forum')->get_content($file);
    $c->stash->{template} = 'forum.tt';
}

sub delete : Local {
    my ($self, $c, $file) = @_;
    $c->stash->{file} = $file;
    $c->stash->{template} ='forum_confirm_del.tt';
}

sub do_delete : Local {
    my ($self, $c, $file) = @_;
    my $loc = Path::Class::Dir->new($c->config->{forum})->file("$file");
    unlink $loc;
    $c->res->redirect($c->uri_for('/forum', {deleted => $file}));
}

sub contribute : Local {
    my ($self, $c) = @_;
    $c->forward('captcha_check');
    if ($c->stash->{recaptcha_ok} ne '1') {
        $c->stash->{my_story} = $c->req->param('page_content');
        $c->stash->{recaptcha_err} = $c->stash->{recaptcha_ok};
        $c->detach('index');
    }
    else {
        # untaint story
        # write story
        $c->model('Forum')->write_content($c->req->param('name'),
                                          $c->req->param('email'),
                                          $c->req->param('page_content'));
        $c->res->redirect($c->uri_for('/forum', {story => 'added'}));
    }
}

sub rss : Local {
    my ($self, $c) = @_;
    my $rss = $c->model('Forum')->make_rss_feed($c->config->{forum}, $c->uri_for('/'));
    $c->res->content_type('application/rss+xml');
    $c->res->body($rss);
}



=head1 AUTHOR

Kieren Diment

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
